<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div class="cp"><el-button type="warning" @click="addPost" icon="el-icon-plus">添加文章</el-button></div>
      <div class="ml-2 cp"><el-button type="danger" icon="el-icon-delete">批量删除</el-button></div>
    </div>
    <el-table :data="forumpostList" border stripe>
      <el-table-column align="center" label="序号" type="index" width="65" />
      <el-table-column align="center" label="所属分类" prop="category_name" width="130" />
      <el-table-column align="center" label="标题" prop="title" width="180" />
      <el-table-column align="center" label="简介" filter-placement prop="introd" :formatter="formatIntrod" />
      <el-table-column align="center" label="添加时间" prop="create_time" width="160" />
      <el-table-column align="center" label="排序" prop="sort" width="65" />
      <el-table-column align="center" label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="flex-1 mt-3">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>
  </d2-container>
</template>

<script>
  import {
    forumpostListUrl,
    forumpostDeleteUrl
  } from '@/utils/forum'
  export default {
    name: 'index',
    data() {
      return {
        queryInfo: {
          page: 1,
          page_size: 10
        },
        forumpostList: [],
        total: 0
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getForumpostList()
      },
      formatIntrod(row, column, cellValue) {
        // 去除 <p> 标签
        const text = cellValue.replace(/<p>/g, '').replace(/<\/p>/g, '');
        return text;
      },
      getForumpostList() {
        this.$get(forumpostListUrl, this.queryInfo).then(res => {
          this.forumpostList = res.data.data.list
          this.total = res.data.data.total
        })
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.page_size = newSize
        this.getForumpostList()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getForumpostList()
      },
      addPost() {
        this.$router.push('/forum/addPost');
      },
      handleEdit(postId) {
        this.$router.push({
          path: '/forum/addPost',
          query: {
            postId: postId
          }
        })
      },
      handleDelete(postId) {
        this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$del(forumpostDeleteUrl + postId + '/').then(res => {
            if (res.data.code == 200) {
              this.getForumpostList()
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>
